package com.shijiwei.lifthelper.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.shijiwei.lifthelper.R;
import com.shijiwei.lifthelper.adapter.ManagerCityAdapter;
import com.shijiwei.lifthelper.bean.City;
import com.shijiwei.lifthelper.utils.DbUtils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sjw on 2015/12/16.
 */
public class CityManagerActivity extends AppCompatActivity {

    @ViewInject(R.id.iv_addcity)
    private ImageView add;
    @ViewInject(R.id.lv_citys)
    private ListView listView;

    public static final int REQUST_ADD = 1;
    private List<City> list;
    private ManagerCityAdapter adapter;
    private DbManager db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_citymanager);
        x.view().inject(this);


        addListener();
        initData();
    }

    private void initData() {
        db = DbUtils.getDb();
        list = new ArrayList<>();
        adapter = new ManagerCityAdapter(list,this);
        listView.setAdapter(adapter);

        try {
            List<City> all = db.findAll(City.class);
            if (all != null){
                list.addAll(all);
            }
            adapter.notifyDataSetChanged();
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    private void addListener() {

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(CityManagerActivity.this,WeatherAddCityActivity.class),REQUST_ADD);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent("com.shijiwei.lifthelper.ADDCITY");
                TextView textView = (TextView) view.findViewById(R.id.tv_city);
                intent.putExtra("city", textView.getText().toString());
                CityManagerActivity.this.sendBroadcast(intent);
                finish();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    db.delete(list.get(position));
                    List<City> all = db.findAll(City.class);
                    list.clear();
                    list.addAll(all);
                    adapter.notifyDataSetChanged();
                } catch (DbException e) {
                    e.printStackTrace();
                }


                return true;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUST_ADD){
            if (resultCode == RESULT_OK){
                City city = new City();
                city.setCity(data.getStringExtra("city"));
                try {
                    db.saveOrUpdate(city);
                    List<City> all = db.findAll(City.class);
                    list.clear();
                    list.addAll(all);
                    adapter.notifyDataSetChanged();

                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
